﻿using System;
using System.Linq;
using System.Web.UI.WebControls;

namespace UI
{
    public partial class TeacherInfoSearch : System.Web.UI.Page
    {
        KCJBSDataContext DB = new KCJBSDataContext();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindProvince();
                bindSchoolName();
                bindselectTitle();
                bindselectDegree();
                bindMajor();
                //bindGvTeachersSearch1();
            }
        }

        /// <summary>
        /// 绑定学位
        /// </summary>
        private void bindselectDegree()
        {
            selectDegree.DataSource = DB.teachers0229.Where(T => T.schoolID == ddlSchoolName.SelectedItem.Value).Select(T => T.degree).Distinct();
            selectDegree.DataBind();
        }
        /// <summary>
        /// 绑定职称
        /// </summary>
        private void bindselectTitle()
        {
            selectTitle.DataSource = DB.teachers0229.Where(T => T.schoolID == ddlSchoolName.SelectedItem.Value).Select(T => T.title).Distinct();
            selectTitle.DataBind();
        }
        /// <summary>
        /// 绑定专业领域
        /// </summary>
        private void bindMajor()
        {
            selectMajor.DataSource = DB.teachers0229.Where(T => T.schoolID == ddlSchoolName.SelectedItem.Value).Select(T => T.major).Distinct();
            selectMajor.DataBind();
        }
        /// <summary>
        /// 绑定省份
        /// </summary>
        private void bindProvince()
        {
            ddlProvince.DataSource = DB.vi_1.Select(V => V.province).Distinct();
            ddlProvince.DataBind();
            ddlSchoolName.Items.Insert(0, "请选择学校名称");
        }

        protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            bindSchoolName();

            bindselectTitle();
            bindselectDegree();
            bindMajor();
        }

        private void bindSchoolName()
        {
            ddlSchoolName.Enabled = true;
            ddlSchoolName.DataSource = DB.Schools.Where(S => S.province.Equals(ddlProvince.SelectedItem.Text)).Select(S => new { S.schoolName, S.schoolID }).Distinct();
            ddlSchoolName.DataTextField = "schoolName";
            ddlSchoolName.DataValueField = "schoolID";
            ddlSchoolName.DataBind();
        }
        protected void ddlSchoolName_SelectedIndexChanged(object sender, EventArgs e)
        {
            bindselectTitle();
            bindselectDegree();
            bindMajor();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Server.Transfer("~/EditorBooksview.aspx");
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            bindGvTeachersSearch1();
        }

        private void bindGvTeachersSearch1()
        {
            //var query = from T in DB.teachers0229
            //            where T.schoolName == ddlSchoolName.SelectedItem.Text
            //            select new { T.authorID, T.degree, T.schoolID, T.schoolName, T.teacherName, T.title, T.xkName, T.major };
            //gvTeacherInfoSearch.DataSource = query;
            Int32? totalRecords = 0, recTemp = 0;
            DB.sp_GetTeacherInfoSearch111(ddlSchoolName.SelectedItem.Value, AspNetPager1.StartRecordIndex, AspNetPager1.EndRecordIndex, ref totalRecords);
            AspNetPager1.RecordCount = totalRecords ?? 0;
            AspNetPager1.PageSize = 15;

            var query = DB.sp_GetTeacherInfoSearch111(ddlSchoolName.SelectedItem.Value, AspNetPager1.StartRecordIndex, AspNetPager1.EndRecordIndex, ref recTemp);
            gvTeacherInfoSearch.DataSource = query;
            gvTeacherInfoSearch.DataKeyNames = new string[] { "authorID", "schoolID" };
            gvTeacherInfoSearch.DataBind();
            #region 强制换行、正常换行
            //正常换行
            gvTeacherInfoSearch.Attributes.Add("style", "word-break:keep-all;word-wrap:normal");
            //强制换行
            gvTeacherInfoSearch.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
            #endregion
        }

        protected void chkTeacherName_CheckedChanged(object sender, EventArgs e)
        {
            Panel1.Visible = chkTeacherName.Checked;
        }
        protected void chkSchoolName_CheckedChanged(object sender, EventArgs e)
        {
            Panel2.Visible = chkSchoolName.Checked;
        }
        protected void chkTitle_CheckedChanged(object sender, EventArgs e)
        {
            Panel3.Visible = chkTitle.Checked;
        }
        protected void chkDegree_CheckedChanged(object sender, EventArgs e)
        {
            Panel4.Visible = chkDegree.Checked;
        }
        protected void chkMajor_CheckedChanged(object sender, EventArgs e)
        {
            Panel5.Visible = chkMajor.Checked;
        }
        protected void btnInto_Click(object sender, EventArgs e)
        {
            Panel6.Visible = true;
            btnInto.Visible = false;
        }

        /// <summary>
        /// gvTeacherInfoSearch行数据绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvTeacherInfoSearch_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //鼠标悬停变色
                e.Row.Attributes.Add("onmouseover", "oldBG=this.style.backgroundColor;this.style.backgroundColor='#ff5599';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=oldBG;");

                ///行序号
                e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();

                //字符串截取
                string strInput = e.Row.Cells[7].Text;
                e.Row.Cells[7].Text = SubString(strInput, 18);
                e.Row.Cells[7].ToolTip = strInput;
            }
        }

        /// <summary>
        /// 字符串截取
        /// </summary>
        /// <param name="strInput">需要截断的字符串</param>
        /// <param name="nLength">截断字符串的长度</param>
        /// <returns></returns>
        private string SubString(string strInput, int nLength)
        {
            if (strInput.Length <= nLength)
            {
                return strInput;
            }
            return strInput.Substring(0, nLength) + "…";
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            bindGvTeachersSearch1();
        }

        private void bindGvTeachersSearch2()
        {
            #region 获取用户选择的数据
            //姓名、职称、学校名称、学位、研究领域
            string teacherName = (chkTeacherName.Checked) ? txtTeacherName.Text.Trim() : string.Empty;
            string title = (chkTitle.Checked) ? selectTitle.Value : string.Empty;
            string schoolName = (chkSchoolName.Checked) ? ddlSchoolName.SelectedValue : string.Empty;
            string degree = (chkDegree.Checked) ? selectDegree.Value : string.Empty;
            string major = (chkMajor.Checked) ? selectMajor.Value : string.Empty;
            #endregion

            #region 绑定gvTeacherInfoSearch数据
            Int32? totalRecords = 0, resultsTemp = 0; ;
            DB.sp_GetTeacherInfoSearch(teacherName, title, major, degree, schoolName, AspNetPager1.StartRecordIndex, AspNetPager1.EndRecordIndex, ref totalRecords);
            AspNetPager1.RecordCount = totalRecords ?? 0;

            gvTeacherInfoSearch.DataSource = DB.sp_GetTeacherInfoSearch(teacherName, title, major, degree, schoolName,
                                                AspNetPager1.StartRecordIndex, AspNetPager1.EndRecordIndex, ref resultsTemp);
            gvTeacherInfoSearch.DataKeyNames = new string[] { "authorID", "schoolID" };
            gvTeacherInfoSearch.DataBind();
            #endregion
        }

        /// <summary>
        /// 全选、反选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSelectAll_Click(object sender, EventArgs e)
        {
            btnSelectAll.Text = btnSelectAll.Text == "全选" ? "反选" : "全选";

            foreach (GridViewRow gr in gvTeacherInfoSearch.Rows)
            {
                System.Web.UI.HtmlControls.HtmlInputCheckBox chkItem = gr.Cells[1].FindControl("chkItem") as System.Web.UI.HtmlControls.HtmlInputCheckBox;
                //相互变换
                chkItem.Checked = !chkItem.Checked;
            }
        }



    }
}

